package com.formiaomiao.demain.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.model.WeatherBean;
import com.formiaomiao.demain.util.TimeUtils;

import java.util.List;

public class WeatherAdapter extends RecyclerView.Adapter<WeatherAdapter.ViewHolder> {

    List<WeatherBean.ResultBean.FutureBean> list;
    Context context;

    public WeatherAdapter(List<WeatherBean.ResultBean.FutureBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.weather_item_layout, parent, false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (position == 0) {
            holder.tv_weather_time.setText("今     天");
        } else if (position == 1) {
            holder.tv_weather_time.setText("明     天");
        } else {
            holder.tv_weather_time.setText(list.get(position).getWeek());
        }
        holder.tv_weather_temperature.setText(list.get(position).getTemperature());
        if (TimeUtils.getCurrentTime()) {
            SetImageforiv(list.get(position).getNight(), holder.iv_weather);
            holder.tv_weather.setText(list.get(position).getNight());
        } else {
            SetImageforiv(list.get(position).getDayTime(), holder.iv_weather);
            holder.tv_weather.setText(list.get(position).getDayTime());
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    private void SetImageforiv(String weather, ImageView iv) {
        switch (weather){
            case "多云":
                iv.setImageResource(R.mipmap.duoyun);
                break;
            case "少云":
                iv.setImageResource(R.mipmap.shaoyun);
                break;
            case "晴":
                iv.setImageResource(R.mipmap.qing);
                break;
            case "阴":
                iv.setImageResource(R.mipmap.yin);
                break;
            case "小雨":
                iv.setImageResource(R.mipmap.xiaoyu);
                break;
            case "雨":
                iv.setImageResource(R.mipmap.yu);
                break;
            case "雷阵雨":
                iv.setImageResource(R.mipmap.leizhenyu);
                break;
            case "中雨":
                iv.setImageResource(R.mipmap.zhongyu);
                break;
            case "阵雨":
                iv.setImageResource(R.mipmap.zhenyu);
                break;
            case "零散阵雨":
                iv.setImageResource(R.mipmap.zhenyu);
                break;
            case "零散雷雨":
                iv.setImageResource(R.mipmap.leizhenyu);
                break;
            case "小雪":
                iv.setImageResource(R.mipmap.xiaoxue);
                break;
            case "中雪":
                iv.setImageResource(R.mipmap.zhongxue);
                break;
            case "大雪":
                iv.setImageResource(R.mipmap.daxue);
                break;
            case "雨夹雪":
                iv.setImageResource(R.mipmap.yujiaxue);
                break;
            case "阵雪":
                iv.setImageResource(R.mipmap.zhenxue);
                break;
            case "霾":
                iv.setImageResource(R.mipmap.mai);
                break;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_weather;
        TextView tv_weather_time, tv_weather, tv_weather_temperature;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_weather = itemView.findViewById(R.id.iv_weather);
            tv_weather_time = itemView.findViewById(R.id.tv_weather_time);
            tv_weather = itemView.findViewById(R.id.tv_weather);
            tv_weather_temperature = itemView.findViewById(R.id.tv_weather_temperature);
        }
    }
}
